package me.a_littlesquid.whitelist.command;


import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabExecutor;
import org.bukkit.plugin.Plugin;

import java.util.ArrayList;
import java.util.List;

public class wl implements CommandExecutor , TabExecutor {
    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
        Plugin config=me.a_littlesquid.whitelist.Whitelist.getProvidingPlugin(me.a_littlesquid.whitelist.Whitelist.class);
        if(args.length == 0) {
            sender.sendMessage(ChatColor.AQUA + "——————" + ChatColor.GOLD + "欢迎使用自定义白名单" + ChatColor.AQUA + "——————");
            sender.sendMessage(ChatColor.AQUA + "[" + ChatColor.GOLD + "custom_whitelist" + ChatColor.AQUA + "]: " + ChatColor.YELLOW + "主指令" + ChatColor.WHITE + " /wl");
            sender.sendMessage(ChatColor.AQUA + "[" + ChatColor.GOLD + "custom_whitelist" + ChatColor.AQUA + "]: " + ChatColor.YELLOW + "添加白名单" + ChatColor.WHITE + " /wl add {玩家名}");
            sender.sendMessage(ChatColor.AQUA + "[" + ChatColor.GOLD + "custom_whitelist" + ChatColor.AQUA + "]: " + ChatColor.YELLOW + "重载白名单" + ChatColor.WHITE + " /wl reload");
            sender.sendMessage(ChatColor.AQUA + "[" + ChatColor.GOLD + "custom_whitelist" + ChatColor.AQUA + "]: " + ChatColor.YELLOW + "白名单列表" + ChatColor.WHITE + " /wl list");

        }
        if(args.length ==1){
            String arg_to_string = args[0];
            if(arg_to_string.equalsIgnoreCase("reload")){
                if(sender.hasPermission("wl.reload")){
                    config.saveConfig();
                    config.reloadConfig();
                    sender.sendMessage(ChatColor.GREEN+"重载成功");
                }else{
                    sender.sendMessage(ChatColor.RED+"你并没有权限(wl.reload)执行此命令");
                }

            }
            if(arg_to_string.equalsIgnoreCase("list")){
                if(sender.hasPermission("wl.list")){
                    List<String> playerlist=config.getConfig().getStringList("playerlist");
                    for(String playername:playerlist){
                        sender.sendMessage(ChatColor.AQUA+"玩家名："+ChatColor.WHITE+playername);
                    }
                }else {
                    sender.sendMessage(ChatColor.RED+"你并没有权限(wl.list)执行此命令");
                }
            }

        }
        if(args.length ==2){
            String arg_to_string = args[0];
            String arg_to_string1 = args[1];
            if(arg_to_string.equalsIgnoreCase("add")){
                if(sender.hasPermission("wl.add")){
                    List<String> playerlist= config.getConfig().getStringList("playerlist");
                    playerlist.add(arg_to_string1);
                    config.getConfig().set("playerlist",playerlist);
                    config.saveConfig();
                    config.reloadConfig();
                    sender.sendMessage(ChatColor.GREEN+"白名单添加成功");
                }
                else{
                    sender.sendMessage(ChatColor.RED+"你并没有权限(wl.add)执行此命令");
                }
            }
        }
        return false;
    }

    @Override
    public List<String> onTabComplete(CommandSender sender, Command command, String label, String[] args) {
        if(args.length==1)
        {
            List<String> list = new ArrayList<>();
            list.add("add");
            list.add("list");
            list.add("reload");
            return list;
        }
        return null;
    }


}
